package com.share.rabbitmq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * author:caifan
 * date:2019/8/10
 */
public class Send {
    private static final String QUEUE_NAME = "hello";
    private static final String HOST = "192.168.2.15";
    private static final int PORT = 5672;

    public static void main(String[] args) {
        connect();
    }

    public static void connect() {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(HOST);
        factory.setPort(PORT);
        factory.setUsername("guest");
        factory.setPassword("guest");
        factory.setVirtualHost("");
        factory.setConnectionTimeout(100000);
        try (Connection connection = factory.newConnection();
             Channel channel = connection.createChannel()) {
            channel.queueDeclare(QUEUE_NAME, false, false, false, null);
            for (int i=0;i<10;i++) {
                String message = "Hello PHP" + i;
                channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
